package com.github.permissiondog.cancanpbackend.servlet;

import com.github.permissiondog.cancanpbackend.request.ApproverSwapRequest;
import com.github.permissiondog.cancanpbackend.request.QuestionSwapRequest;
import com.github.permissiondog.cancanpbackend.response.BaseResponse;
import com.github.permissiondog.cancanpbackend.service.TemplateService;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "AdminApproverSwapServlet", value = "/admin/template/approver/swap")
public class AdminApproverSwapServlet extends BaseServlet {
    /**
     * 交换审批人
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
        var body = bind(req, res, ApproverSwapRequest.class);
        if (body == null)   return;

        try {
            TemplateService.getInstance().swapApprover(
                    body.templateId, body.approverId1, body.approverId2);
            json(res, BaseResponse.builder().ok(true).build());
        } catch (Exception e) {
            json(res, BaseResponse.builder().ok(false).msg("交换失败").error(e.getMessage()).build());
        }
    }
}
